class teamIntro {
  constructor(mongoose) {
    this.mongoose = mongoose
    this.Schema = mongoose.Schema
    // AnimeIntro模板
    this.teamIntro = new this.Schema({

      userId: String,
      id: Number,
      name: String,
      content: String,
      time: Date,
      max: Number,
      imgUrl: String,
      participant: Array,
      number: Number,
      avatar: String,

    })
    this.modal = this.mongoose.model('Team', this.teamIntro)
  }

  // @paras1 查询条件 动漫名字数组
  // @paras2 查询结果过滤器
  // 返回查询结果
  teamsFind() {
    var start = new Date()
    start.setHours(0)
    start.setMinutes(0)
    start.setSeconds(0)
    var end = new Date()
    end.setDate(end.getDate() + 7)
    end.setHours(0)
    end.setMinutes(0)
    end.setSeconds(0)
    // return this.modal.find().
    return this.modal.find({ time: { $gte: start, $lte: end } }).sort({ number: 1 })

  }

  teamFind(body) {
    var start = new Date()
    start.setHours(0)
    start.setMinutes(0)
    start.setSeconds(0)
    if (body.userId) {
      return this.modal.find({ participant: { $elemMatch: { $eq: body.userId } },time: { $gte: start } })

    } else {
      return this.modal.find({ userId: body.coachId,time: { $gte: start } })

    }
  }

  addTeam(body) {
    if (body.imgUrl === '') {
      let Term = new this.modal({
        userId: body.userId,
        id: body.id,
        name: body.name,
        content: body.content,
        time: new Date(body.date + ' ' + body.time),
        max: body.max,
        imgUrl: '',
        participant: [],
        number: 0,
        avatar: body.avatar
      })
      return Term.save()
    } else {
      return this.modal.updateOne({ id: body.id }, { imgUrl: body.imgUrl })
    }
  }

  participate(body) {
    return this.modal.updateOne({ id: body.id, participant: { $ne: body.userId } }, { $push: { participant: body.userId }, $inc: { number: 1 } })
  }
}

module.exports = teamIntro
